﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml;

namespace WorldOfMUDra
{
    public class PlayerConfigFile
    {
        string[] _lines;
        readonly string _location;
        readonly string _name;
        readonly List<string> _visitedareas = new List<string>();
        readonly List<string> _itemids;
        readonly List<string> _abilities;

        public PlayerConfigFile(string fileName)
        {
            string fname = fileName;
            if (fname.Split('.')[1].ToLower() == "xml")
            {
                Console.WriteLine("XML file extension detected. Will attempt to parse as XML document...");
                XmlTextReader reader = new XmlTextReader(fname);
                while (reader.Read())
                {
                    switch (reader.NodeType)
                    {
                        case XmlNodeType.Element:
                            switch (reader.Name.ToLower())
                            {
                                case "location":
                                    _location = reader.ReadString().Trim();
                                    break;
                                case "visitedlocations":
                                    _visitedareas = reader.ReadString().Trim().Split(' ').ToList();
                                    break;
                                case "name":
                                    _name = reader.ReadString().Trim();
                                    break;
                                case "itemids":
                                    string ids = reader.ReadString().Trim();
                                    _itemids = new List<string>();
                                    foreach (string id in ids.Split(' ')) _itemids.Add(id);
                                    break;
                                case "abilities":
                                    string abs = reader.ReadString().Trim();
                                    _abilities = new List<string>();
                                    foreach (string ability in abs.Split(' ')) _abilities.Add(ability);
                                    break;
                            }
                            break;
                    }
                }
            }
            try
            {
                _lines = File.ReadAllLines(fname);
            }
            catch (Exception)
            {
                Console.WriteLine("Issues loading player config file by name: " + fname);
            }
        }

        public string Name
        {
            get
            {
                return _name;
            }
        }

        public string LocationId
        {
            get
            {
                return _location;
            }
        }

        public string[] VisitedAreas
        {
            get
            {
                return _visitedareas.ToArray();
            }
        }

        public string[] ItemIDs
        {
            get
            {
                return _itemids.ToArray();
            }
        }

        public string[] Abilities
        {
            get
            {
                return _abilities.ToArray();
            }
        }
    }
}
